package com.xjrsoft.module.ureport.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.ureport.dto.GetRptFilePageListDto;
import com.xjrsoft.module.ureport.dto.RptRelationDto;
import com.xjrsoft.module.ureport.entity.XjrRptFile;
import com.xjrsoft.module.ureport.service.IXjrRptFileService;
import com.xjrsoft.module.ureport.vo.RptFileVo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/reports-file")
@AllArgsConstructor
public class XjrRptFileController {

    private IXjrRptFileService rptFileService;


    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/12/15
     * @Param:[dto]
     * @return:com.xjrsoft.common.result.Response
     * @Description:查询列表数据
     */
    @GetMapping("/page")
    @ApiOperation("获取报表文件列表，分页")
    public Response<PageOutput<RptFileVo>> getPageList(GetRptFilePageListDto dto) {
        IPage<XjrRptFile> page = ConventPage.getPage(dto);
        String keyword = dto.getKeyword();
        IPage<XjrRptFile> resultPage = rptFileService.page(page, Wrappers.<XjrRptFile>query().lambda().like(!StringUtil.isEmpty(keyword), XjrRptFile::getName, keyword));
        return Response.ok(ConventPage.getPageOutput(resultPage, RptFileVo.class));
    }

    @GetMapping
    @ApiOperation("获取报表文件列表，分页")
    public Response<List<RptFileVo>> getList(@RequestParam(required = false) String keyword) {
        List<XjrRptFile> resultList = rptFileService.list(Wrappers.<XjrRptFile>query().lambda().like(!StringUtil.isEmpty(keyword), XjrRptFile::getName, keyword));
        return Response.ok(BeanUtil.copyList(resultList, RptFileVo.class));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除报表文件")
    public Response delete(@PathVariable String id) {
        return Response.status(rptFileService.removeById(id));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/12/15
     * @Param:[dto]
     * @return:com.xjrsoft.common.result.Response
     * @Description:保存报表文件
     */
    @PostMapping
    public Response save(@RequestBody RptRelationDto dto) {
        return Response.status(rptFileService.saveRptFile(dto));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/12/15
     * @Param:[dto]
     * @return:com.xjrsoft.common.result.Response
     * @Description:更新报表文件
     */
    @PutMapping
    public Response update(RptRelationDto dto) {
        return Response.ok();
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/12/11
     * @Param:[id]
     * @return:com.xjrsoft.common.result.Response
     * @Description:获取报表设计文件内容,已url加密
     */
    @GetMapping("/{id}")
    public Response getFileContentById(@PathVariable String id) {
        return Response.ok(BeanUtil.copy(rptFileService.getById(id), RptFileVo.class));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/12/11
     * @Param:[id]
     * @return:java.lang.String
     * @Description:
     */
    @GetMapping("/genXml")
    public Response genXml(@PathVariable String id) {
        XjrRptFile xjrRptFile = rptFileService.getById(id);
        if (xjrRptFile != null) {
            String content = String.valueOf(xjrRptFile.getContent());
//          WriteKit.write(springboot_path + File.separator + "src" + File.separator + "main" + File.separator + "webapp" + File.separator + "WEB-INF" + File.separator + "ureportfiles"
//                    + File.separator + xjrRptFile.getName(), text, "utf-8");

        }
        return Response.notOk();
    }

    /**
     * type_ : 0 全部文件 1未绑定文件 2已绑定文件 pnum : psize :
     *
     * @return
     */
    @RequestMapping(value = "/getFiles")
    public Response getFiles(GetRptFilePageListDto dto) {
        String type = dto.getType();
        List<XjrRptFile> list = new ArrayList<XjrRptFile>();
        switch (type) {
            case "0":
                // 全部文件
                list = rptFileService.list();
                break;
            case "1":
                // 未绑定文件
                // xjr_ureport_fileService.getFilesUnbind(pnum,psize);
                break;
            case "2":
                // 已绑定文件
                // xjr_ureport_fileService.getFilesBinded(pnum,psize);
                break;
            default:
                break;
        }
        return Response.ok(list);
    }
}
